﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Classes;
using SubSonic;
using System.Data;
using System.Text;

namespace AvaliacaoWeb
{
    public partial class Lista : System.Web.UI.Page
    {
        private String DecodeFromUtf8(String utf8_String)
        {
            byte[] bytes = Encoding.Default.GetBytes(utf8_String);
            return Encoding.UTF8.GetString(bytes);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnGravar.Attributes.Add("onclick", "document.getElementById('imgCarregando').style.display='block'; this.disabled = true;" + ClientScript.GetPostBackEventReference(btnGravar, null) + ";");
                btnVoltar.Attributes.Add("onclick", "document.getElementById('imgCarregando').style.display='block'; this.disabled = true;" + ClientScript.GetPostBackEventReference(btnVoltar, null) + ";"); 

                /// Verific se ainda possúi alguma avaliação disponível
                btnGravar.Enabled = true;
                String sqlAvaliacoesDisp = " Select count(*) From a_avaliacao a" +
                                 " inner join a_quest q " +
                                    " On q.numava =  a.numava" +
                                    " and q.numqts = a.numqts " +
                                 " where a.numava = " + Request.QueryString["numava"].ToString() +
                                 " and a.resp = '' and a.matric = " + Session["Matric"].ToString() +
                                 " and q.codcon != ''";

                QueryCommand qryCommand = new QueryCommand(sqlAvaliacoesDisp, "Default");

                DataTable dt = new DataTable();
                dt = DataService.GetDataSet(qryCommand).Tables[0];

                if (int.Parse(dt.Rows[0][0].ToString()) > 0)
                    btnGravar.Enabled = false;
                // ======================================================= \\

                if (Request.QueryString["numava"] == null)
                    Response.Redirect("SelecionaAvaliacao.aspx");

                AIdava ava = new AIdava(Request.QueryString["numava"].ToString());
                lblTitulo.Text = ava.Titulo;

                String sql = " SELECT a_avaliacao.numava, a_avaliacao.avaliado, " + 
                            " if(isNull(a_discip.desdis), a_avaliacao.avaliado, concat(a_avaliacao.avaliado, ' - ', a_discip.desdis)) as avaliadodis " +
                            " FROM a_avaliacao " +
                            " LEFT JOIN a_discip " +
                                " ON a_avaliacao.discip = a_discip.coddis " +
                            " WHERE numava = " + Request.QueryString["numava"].ToString() +
                            " AND matric = " + Session["Matric"].ToString() +
                            " GROUP BY numava, avaliado";

                qryCommand = new QueryCommand(sql, "Default");

                dt = DataService.GetDataSet(qryCommand).Tables[0];

                dtGrid.DataSource = dt;
                dtGrid.DataBind();
            }
        }

        protected void btnVoltar_Click(object sender, EventArgs e)
        {
            Response.Redirect("SelecionaAvaliacao.aspx");
        }

        protected void dtGrid_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                TextBox numava = e.Row.FindControl("numava") as TextBox;
                TextBox avaliado = e.Row.FindControl("avaliado") as TextBox;
                String sql = " Select count(*) From a_avaliacao where numava = " + numava.Text.Trim() + " and avaliado = '" + DecodeFromUtf8(avaliado.Text.Trim()) + "'" +
                             " and resp != '' and matric = " + Session["Matric"].ToString();

                QueryCommand qryCommand = new QueryCommand(sql, "Default");

                DataTable dt = new DataTable();
                dt = DataService.GetDataSet(qryCommand).Tables[0];

                if (int.Parse(dt.Rows[0][0].ToString()) > 0)
                {
                    e.Row.Cells[1].Controls.Clear();                    
                    e.Row.Cells[1].Text = "-Avaliado-";
                    e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Center;                                          
                }else
                    e.Row.Cells[1].Attributes.Add("onclick", "document.getElementById('imgCarregando').style.display='block';");
            }


        }

        protected void btnGravar_Click(object sender, EventArgs e)
        {

            String sql = "Update a_avaliador set flgava = 'R'" +
                         " Where numava = " + Request.QueryString["numava"].ToString() +
                         " And matric = " + Session["Matric"].ToString();

            QueryCommand qry = new QueryCommand(sql, "Default");
            DataService.ExecuteQuery(qry);

            Response.Redirect("SelecionaAvaliacao.aspx");
        }
    }
}